﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using System.Collections;
using ExpertMIS.Entities;

namespace ExpertMIS.Forums
{
    public partial class ExpertList : System.Web.UI.Page
    {
        private string _currentDomainOID = string.Empty;
        private DomainBroker _domainBroker = new DomainBroker();
        
        private void RefreshDomains()
        {
            dlDomains.DataSource = _domainBroker.GetAllDomains();
            dlDomains.DataBind();
        }

        private void RefreshDomainIntroduce(string DomainOID)
        {
            DomainBroker domainBroker = new DomainBroker();
            Hashtable htKeys = new Hashtable();

            htKeys["OID"] = DomainOID;
            DomainEntity entity = domainBroker.GetEntity(htKeys) as DomainEntity;

            _currentDomainOID = DomainOID;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ucBulletinList1.CSS = "expert-list-bulletin";
            ucBulletinList1.BulletinCatalog = ExpertMIS.UserControl.BulletinCatalog.SocailBulletin;
            ucBulletinList1.LengthVariable = "ExpertList.BulletinTitle.Length";
            RefreshDomainIntroduce(Request["oid"]);
            RefreshDomains();
        }

        protected void dlDomains_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DomainEntity domain = e.Item.DataItem as DomainEntity;
                HyperLink hlDomain = e.Item.FindControl("hlDomain") as HyperLink;

                if (domain != null && hlDomain != null)
                {
                    hlDomain.Text = domain.DomainName;

                    if (domain.OID == _currentDomainOID)
                    {
                        hlDomain.Enabled = false;
                        hlDomain.Font.Bold = true;
                    }
                    else
                    {
                        hlDomain.NavigateUrl = "DomainIntroduce.aspx?oid=" + domain.OID;
                    }
                }

                Image imgDomainIcon = e.Item.FindControl("imgDomainIcon") as Image;
                if (domain != null && imgDomainIcon != null)
                {
                    string imgUrl = ExpertMIS.Codes.Utility.GetEnvironmentValue(domain.OID + ".icon");
                    if (!string.IsNullOrEmpty(imgUrl))
                    {
                        imgDomainIcon.ImageUrl = imgUrl;
                    }
                }
            }
        }
    }
}
